package com.quanquan.store.repository;

import com.quanquan.store.entity.ShoppingKillTradingRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: ShoppingKillTradingRecordRepository
 * @Author: xiaoqianh丶
 * @Date: 2019-07-01 15:00
 * @Version: V1.0
 */
public interface ShoppingKillTradingRecordRepository extends JpaRepository<ShoppingKillTradingRecord,String>, JpaSpecificationExecutor<ShoppingKillTradingRecord> {



    @Query(value = "SELECT * FROM oo_shoppingkill_trading_record WHERE trading_status = 'unpaid'", nativeQuery = true)
    public List<ShoppingKillTradingRecord> findAllUnpaid();


    /**
     * 根据秒杀商品id查询记录
     * @param shoppingKillId
     * @return
     */
    @Query(value = "SELECT * FROM oo_shoppingkill_trading_record WHERE trading_status = 'paid'", nativeQuery = true)
    public List<ShoppingKillTradingRecord> findByShoppingKillId(String shoppingKillId);

    /**
     * 根据秒杀商品id查询记录(用户的个人的)
     * @param
     * @return
     */
    public List<ShoppingKillTradingRecord> findByUseId(String userId);


    /**
     * 查询用户秒杀成功次数
     * @param userId
     * @param shoppingKillId
     * @return
     */
    @Query(value = "SELECT count(*) FROM oo_shoppingkill_trading_record WHERE trading_status = 'paid' and use_id =?1 and shopping_kill_id =?2", nativeQuery = true)
    public Integer findAllCount(String userId, String shoppingKillId);


    @Query(value = "SELECT count(*) FROM oo_shoppingkill_trading_record WHERE trading_status = 'paid' and use_id =?1 and shopping_kill_id =?2 and create_time >?3", nativeQuery = true)
    public Integer findDailyCount(String userId, String shoppingKillId, String startTime);
}
